package com.stlm2.controller;

import com.stlm2.dems.dao.PriceRecordDao;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.DdmGoods;
import com.stlm2.dems.entity.GoodSpecification;
import com.stlm2.dems.entity.PriceRecord;
import com.stlm2.dems.entity.User;

import com.stlm2.dems.service.DdmGoodsService;
import com.stlm2.dems.service.GoodSpecificationService;
import com.stlm2.dems.service.PriceRecordService;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author：fuchunyu
 * Date：2017/9/11
 * Description：
 */
@Controller
@RequestMapping("/goodSpecificationC")
public class GoodSpecificationController {

    @Resource
    private PriceRecordService priceRecordService;
    @Resource
    private UserDao userDao;
    @Resource
    private GoodSpecificationService goodSpecificationService;
    @Resource
    private DdmGoodsService ddmGoodsService;
    @Resource
    private PriceRecordDao priceRecordDao;


    @RequestMapping(value="/listView",produces="application/json;charset=UTF-8")
    public ModelAndView listView(ModelAndView modelAndView){
        modelAndView.setViewName("/goodSpecification/list");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "/addGoodSpecification.json",produces = "application/json;charset=UTF-8")
    public String addGoodSpecification(String token, Integer goodId, String propertiesIds, String propertiesNames, String image, String purchasePrice,String costPrice,String ddPrice,String memberPrice,String parameter){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("goodid",goodId);
        map.put("propertiesid",propertiesIds);

        DdmGoods goods=ddmGoodsService.findEntityById(goodId);
        if (goods==null)
            return CodeContent.returnFailure("没有找到商品！");
        if(goods.getState())
            return CodeContent.returnFailure("请先下架商品再做修改！");

        GoodSpecification gs=goodSpecificationService.getGoodSpecification(map);
        if(gs!=null)
            return CodeContent.returnFailure("该规格组合已添加！");

        User user=userDao.getByToken(token);
        if(user==null)
            return CodeContent.returnLogin();
        GoodSpecification goodSpecification=new GoodSpecification();
        if(goodId==null&&goodId<=0)
            return CodeContent.returnFailure("非法的商品ID！");
        if(StringUtils.isEmpty(propertiesIds))
            return CodeContent.returnFailure("规格组合ID为空！");
        if(StringUtils.isEmpty(propertiesNames))
            return CodeContent.returnFailure("规格组合内容为空！");
        if(StringUtils.isEmpty(purchasePrice))
            return CodeContent.returnFailure("进货价为空！");
        if(StringUtils.isEmpty(costPrice))
            return CodeContent.returnFailure("成本价为空！");
        if(StringUtils.isEmpty(memberPrice))
            return CodeContent.returnFailure("会员价为空！");
        if(StringUtils.isEmpty(ddPrice))
            return CodeContent.returnFailure("叮当价为空！");
        if(StringUtils.isEmpty(parameter))
            return CodeContent.returnFailure("规格基本参数为空！");

        goodSpecification.setCreateTime(new Date());
        goodSpecification.setCreateUserId(user.getId());
        goodSpecification.setGoodId(goodId);
        goodSpecification.setGoodsState(true);
        if(StringUtils.isNotEmpty(image))
            goodSpecification.setImage(image);
        goodSpecification.setPurchasePrice(new BigDecimal(purchasePrice));
        goodSpecification.setCostPrice(new BigDecimal(costPrice));
        goodSpecification.setMemberPrice(new BigDecimal(memberPrice));
        goodSpecification.setDdPrice(new BigDecimal(ddPrice));
        goodSpecification.setPropertiesId(propertiesIds);
        goodSpecification.setPropertiesName(propertiesNames);
        goodSpecification.setParameter(parameter.replace("https://m.9ji.com",""));

        //添加价格走势
        PriceRecord priceRecord=new PriceRecord();
        priceRecord.setGoodsId(goodSpecification.getGoodId());
        priceRecord.setPrice(goodSpecification.getMemberPrice());
        priceRecord.setSpeciflcationId(goodSpecification.getId());
        priceRecord.setUpdateTime(new Date());
        priceRecord.setUpdateUserId(user.getId());
        priceRecordService.addEntity(priceRecord);

        goodSpecificationService.addEntity(goodSpecification);

        return CodeContent.returnSuccess(null);
    }



    @ResponseBody
    @RequestMapping(value = "/getGoodSpecificationAll.json",produces = "application/json;charset=UTF-8")
    public String getGoodSpecification(Integer goodid){
        List<GoodSpecification> list=goodSpecificationService.getByGoodId(goodid);
        return CodeContent.returnSuccess(goodSpecificationService.conventAll(list));
    }

    @ResponseBody
    @RequestMapping(value = "/detailsGoodSpecification.json",produces = "application/json;charset=UTF-8")
    public String detailsGoodSpecification(Integer id){
        GoodSpecification goodSpecification  = goodSpecificationService.findEntityById(id);
        return CodeContent.returnSuccess(goodSpecificationService.builderModelAll(goodSpecification));
    }

    @ResponseBody
    @RequestMapping(value = "/updateGoodSpecification.json",produces = "application/json;charset=UTF-8")
    public String updateGoodSpecification(String id,String image, String purchasePrice,String costPrice,String ddPrice,String memberPrice,String parameter,String token){
        User user=userDao.getByToken(token);
        if(user==null)
            return CodeContent.returnLogin();
        if(!NumberUtils.isDigits(id))
            return CodeContent.returnFailure("非法的ID！");
        GoodSpecification goodSpecification=goodSpecificationService.findEntityById(Integer.parseInt(id));
        if(StringUtils.isNotEmpty(image))
            goodSpecification.setImage(image);
        if(StringUtils.isNotEmpty(purchasePrice))
            goodSpecification.setPurchasePrice(new BigDecimal(purchasePrice));
        if(StringUtils.isNotEmpty(costPrice))
            goodSpecification.setCostPrice(new BigDecimal(costPrice));
        if(StringUtils.isNotEmpty(ddPrice))
            goodSpecification.setDdPrice(new BigDecimal(ddPrice));
        if(StringUtils.isNotEmpty(memberPrice)){
            goodSpecification.setMemberPrice(new BigDecimal(memberPrice));
            //添加价格走势  价格不一样就添加
            PriceRecord priceRecord1=new PriceRecord();
            priceRecord1.setGoodsId(goodSpecification.getId());
            priceRecord1.setSpeciflcationId(goodSpecification.getId());
            priceRecord1=priceRecordDao.getRecentlyOne(priceRecord1);
            if(priceRecord1==null ){
                PriceRecord priceRecord=new PriceRecord();
                priceRecord.setGoodsId(goodSpecification.getGoodId());
                priceRecord.setPrice(goodSpecification.getMemberPrice());
                priceRecord.setSpeciflcationId(goodSpecification.getId());
                priceRecord.setUpdateTime(new Date());
                priceRecord.setUpdateUserId(user.getId());
                priceRecordService.addEntity(priceRecord);
            }

            if(goodSpecification.getMemberPrice().compareTo(new BigDecimal(memberPrice))!=0){
                PriceRecord priceRecord=new PriceRecord();
                priceRecord.setGoodsId(goodSpecification.getGoodId());
                priceRecord.setPrice(goodSpecification.getMemberPrice());
                priceRecord.setSpeciflcationId(goodSpecification.getId());
                priceRecord.setUpdateTime(new Date());
                priceRecord.setUpdateUserId(user.getId());
                priceRecordService.addEntity(priceRecord);
            }
        }
        if(StringUtils.isNotEmpty(parameter))
            goodSpecification.setParameter(parameter.replace("https://m.9ji.com",""));

        goodSpecificationService.updateEntity(goodSpecification);

        return CodeContent.returnSuccess(null);
    }

    /**
     * Author：fuchunyu
     * Date：2017/9/12
     * Description：添加规格商品库存
     */

    @ResponseBody
    @RequestMapping(value = "/addGoodSpecificationInventory.json",produces = "application/json;charset=UTF-8")
    public String addGoodSpecificationInventory(String inventory,String id){
        if(!NumberUtils.isDigits(id))
            return CodeContent.returnFailure("非法的ID");
        if(!NumberUtils.isDigits(inventory))
            return CodeContent.returnFailure("非法的库存");
        GoodSpecification goodSpecification=goodSpecificationService.findEntityById(Integer.parseInt(id));
        if(goodSpecification==null)
            CodeContent.returnFailure("获取规格组合失败！");
        //goodSpecification.setIntegral(goodSpecification.getInventory()+Integer.parseInt(inventory));
        //goodSpecification.setTotalInventory(goodSpecification.getTotalInventory()+Integer.parseInt(inventory));


        DdmGoods goods=ddmGoodsService.findEntityById(goodSpecification.getGoodId());
        if(goods==null)
            return CodeContent.returnFailure("商品为空！");
        //goods.setInventory(goods.getIntegral()+Integer.parseInt(inventory));
        //goods.setTotalInventory(goods.getTotalInventory()+Integer.parseInt(inventory));

        goodSpecificationService.updateEntity(goodSpecification);
        ddmGoodsService.updateEntity(goods);

        return CodeContent.returnSuccess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/getGoodSpecification.json",produces="application/json;charset=UTF-8")
    public String getGoodSpecification(Integer goodid,String propertiesid){

        Map<String,Object> map=new HashMap<String,Object>();
        map.put("goodid",goodid);
        map.put("propertiesid",propertiesid);

        GoodSpecification goodSpecification=goodSpecificationService.getGoodSpecification(map);
        if(goodSpecification==null)
            return CodeContent.returnFailure("没有添加此规格组合！");
        return CodeContent.returnSuccess(goodSpecificationService.builderModel(goodSpecification));
    }

    @ResponseBody
    @RequestMapping(value = "/getGoodSpecificationAll2.json",produces = "application/json;charset=UTF-8")
    public String getGoodSpecification2(Integer goodid){
        List<GoodSpecification> list=goodSpecificationService.getByGoodId(goodid);
        return CodeContent.returnSuccess(goodSpecificationService.conventAll2(list));
    }

}
